/**
 * Copyright (C), 2020-2021 by colley.ma
 * File Name: FutureTimeAdapter.java
 * Encoding: UTF-8
 * Date: 2021年4月2日 下午2:37:59
 * History:
*/
package com.swak.frame.api.filter.extension;

import java.util.Date;
import java.util.Optional;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import com.swak.frame.config.ConfigCenterAdapter;
import com.swak.frame.extension.annotation.Extension;
import com.swak.frame.extension.provider.FutureClockTimeExtPtProvider;
import com.swak.frame.util.DFormatEnum;
import com.swak.frame.util.EmptyObject;

@Extension(desc = "时间穿越")
public class DefaultFClockTimeProvider implements FutureClockTimeExtPtProvider {

	@Autowired(required = false)
	private ConfigCenterAdapter configCenterAdapter;

	@Override
	public Set<String> whiteListConfig() {
		if (configCenterAdapter == null) {
			return EmptyObject.emptySet();
		}
		String previewWhiteList = configCenterAdapter.getString(CLOCKTIME_WHITE_LIST);
		if (StringUtils.isEmpty(previewWhiteList)) {
			return EmptyObject.emptySet();
		}
		return Sets.newHashSet(Splitter.on(',').splitToList(previewWhiteList));
	}

	@Override
	public Date getClockTime() {
		Date mockDate = configCenterAdapter.getDate(FUTURE_CONFIG_TIME, DFormatEnum.YYYYMMDD_SLASH_FULL.pattern);
		return Optional.ofNullable(mockDate).orElse(new Date());
	}
}
